package com.xvyy.tingshu.order.adapter.impl;

import com.xvyy.tingshu.common.constant.SystemConstant;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.model.user.VipServiceConfig;
import com.xvyy.tingshu.order.adapter.OrderDealTypeAdapter;
import com.xvyy.tingshu.order.mapper.OrderInfoMapper;
import com.xvyy.tingshu.user.client.VipServiceConfigFeignClient;
import com.xvyy.tingshu.vo.order.OrderDerateVo;
import com.xvyy.tingshu.vo.order.OrderDetailVo;
import com.xvyy.tingshu.vo.order.OrderInfoVo;
import com.xvyy.tingshu.vo.order.TradeVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: OrderDealTypeByVip
 * Package: com.xvyy.tingshu.order.adapter.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/16 - 0:17
 * @Version: v1.0
 */
@Component
@Slf4j
public class OrderDealTypeByVip implements OrderDealTypeAdapter {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Override
    public Boolean supportOrderDealType(String dealType) {
        return SystemConstant.ORDER_ITEM_TYPE_VIP.equals(dealType);
    }

    @Override
    public OrderInfoVo adapterOrderDealType(TradeVo tradeVo, Long userId, String itemType) {

        OrderInfoVo orderInfoVoResult = new OrderInfoVo();
        // 1. 获取itemId
        Long itemId = tradeVo.getItemId();
        // 2. 校验付款项vip是否存在支付中
        int count = orderInfoMapper.checkAlbumAndVipTypeItemPadding(itemType, userId, itemId);
        if (count != 0) {
            log.error("该付款项vip套餐：{}处于购买中，请稍后再试",itemId);
            orderInfoVoResult.setExitItemIdList(Lists.newArrayList(itemId));
            return orderInfoVoResult;
        }
        // 3. 给OrderInfoVo赋值
        // 3.1 订单号
        String tradeNo = RandomStringUtils.random(15, true, true);
        orderInfoVoResult.setTradeNo(tradeNo);
        // 3.2 支付方式
        orderInfoVoResult.setPayWay("");
        // 3.3 交易类型
        orderInfoVoResult.setItemType(itemType);
        // 3.4 原始金额
        Result<VipServiceConfig> vipServiceConfigResult = vipServiceConfigFeignClient.getAllVipConfig(itemId);
        VipServiceConfig vipServiceConfigData = vipServiceConfigResult.getData();
        Assert.notNull(vipServiceConfigData, "远程调用vip微服务，获取会员配置失败");
        BigDecimal originalAmount = vipServiceConfigData.getPrice();
        orderInfoVoResult.setOriginalAmount(originalAmount.setScale(2, RoundingMode.HALF_UP));
        BigDecimal orderAmount = vipServiceConfigData.getDiscountPrice();
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);
        // 3.5 减免金额
        orderInfoVoResult.setDerateAmount(derateAmount.setScale(2, RoundingMode.HALF_UP));
        // 3.6 实付金额
        orderInfoVoResult.setOrderAmount(orderAmount.setScale(2, RoundingMode.HALF_UP));
        // 3.7 订单详情列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(vipServiceConfigData.getName());
        orderDetailVo.setItemUrl(vipServiceConfigData.getImageUrl());
        orderDetailVo.setItemPrice(orderAmount.setScale(2, RoundingMode.HALF_UP));
        orderDetailVoList.add(orderDetailVo);
        orderInfoVoResult.setOrderDetailVoList(Lists.newArrayList(orderDetailVoList));
        // 3.8 订单减免列表
        ArrayList<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        OrderDerateVo orderDerateVo = new OrderDerateVo();
        orderDerateVo.setDerateType("VIP服务折扣");
        orderDerateVo.setDerateAmount(derateAmount);
        orderDerateVo.setRemarks("订单减免类型1406-VIP服务折扣");
        orderDerateVoList.add(orderDerateVo);
        orderInfoVoResult.setOrderDerateVoList(Lists.newArrayList(orderDerateVoList));
        // 4. 返回结果
        return orderInfoVoResult;
    }
}
